#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> 

#include "setup_gui.h"
#include "utils.h"
#include "main_gui.h"

Etk_Widget *msgdialog;

Setup_Gui *setup_gui;

void build_setup_gui(void) {
    setup_gui = calloc(1, sizeof(Setup_Gui));

    // Main window
    setup_gui->win = etk_window_new();
    etk_window_title_set(ETK_WINDOW(setup_gui->win), "E-Mail setup wizard");
    //etk_window_resize(ETK_WINDOW(win), 500, 400);	// Autofit
    etk_container_border_width_set(ETK_CONTAINER(setup_gui->win), 5);
    etk_window_center_on_window(ETK_WINDOW(setup_gui->win), NULL);
    etk_signal_connect("destroyed", ETK_OBJECT(setup_gui->win), ETK_CALLBACK(_setup_quit_cb), NULL);
    // End main window

    // Main vbox
    setup_gui->vbox = etk_vbox_new(ETK_FALSE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->win), setup_gui->vbox);
    // End main vbox

    // Button area and buttons
    setup_gui->btn_hbox = etk_hbox_new(ETK_TRUE,3);
    etk_box_append(ETK_BOX(setup_gui->vbox), setup_gui->btn_hbox, ETK_BOX_END, ETK_BOX_NONE, 3);

    setup_gui->btn_cancel = etk_button_new_with_label(" Cancel ");
    etk_signal_connect("clicked", ETK_OBJECT(setup_gui->btn_cancel), ETK_CALLBACK(_setup_quit_cb), NULL);
    setup_gui->btn_ok = etk_button_new_with_label("   OK   ");
    etk_signal_connect("clicked", ETK_OBJECT(setup_gui->btn_ok), ETK_CALLBACK(_btn_ok_cb), (void *)etk_entry_text_get(ETK_ENTRY(setup_gui->mbox_entry)));
    etk_box_append(ETK_BOX(setup_gui->btn_hbox),setup_gui->btn_cancel, ETK_BOX_END, ETK_BOX_NONE, 3);
    etk_box_append(ETK_BOX(setup_gui->btn_hbox),setup_gui->btn_ok, ETK_BOX_END, ETK_BOX_NONE, 3);
    // End button area and buttons

    // Mailbox location frame	
    setup_gui->frame_mailbox = etk_frame_new(" Mailbox location ");
    etk_box_append(ETK_BOX(setup_gui->vbox), setup_gui->frame_mailbox, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);

    setup_gui->frame_mailbox_vbox = etk_vbox_new(ETK_FALSE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_mailbox), setup_gui->frame_mailbox_vbox);

    setup_gui->label = etk_label_new("First, you have to specify the location of mailbox.<br>"
                                                             "It will be created under your home directory.<br>"
                                                             "If you are unsure, please use this suggestion.");
    etk_box_append(ETK_BOX(setup_gui->frame_mailbox_vbox), setup_gui->label, ETK_BOX_NONE, ETK_BOX_NONE, 3);

    setup_gui->mbox_entry = etk_entry_new();
    etk_entry_text_set(ETK_ENTRY(setup_gui->mbox_entry), "Mail");
    etk_box_append(ETK_BOX(setup_gui->frame_mailbox_vbox), setup_gui->mbox_entry, ETK_BOX_NONE, ETK_BOX_NONE, 3);
    etk_signal_connect("text-changed", ETK_OBJECT(setup_gui->mbox_entry), ETK_CALLBACK(_mbox_entry_text_changed), (void *)etk_entry_text_get(ETK_ENTRY(setup_gui->mbox_entry)));

    setup_gui->chkbtn_skip_account_setup = etk_check_button_new_with_label("Skip account settings?");
    etk_box_append(ETK_BOX(setup_gui->frame_mailbox_vbox), setup_gui->chkbtn_skip_account_setup, ETK_BOX_NONE, ETK_BOX_NONE, 3);
    etk_signal_connect("toggled", ETK_OBJECT(setup_gui->chkbtn_skip_account_setup), ETK_CALLBACK(_skip_account_setup_cb), NULL);
    setup_gui->label = etk_label_new("If you check this you can skip the creation of <br>"
                                                             "first account. E-Mail starts, but you are only <br>"
                                                             "able to browse your mailbox. <br>"
                                                             "You can create accounts later from menu.");
    etk_box_append(ETK_BOX(setup_gui->frame_mailbox_vbox), setup_gui->label, ETK_BOX_NONE, ETK_BOX_NONE, 3);
    // End mailbox location frame

    // Account setting frame
    setup_gui->frame_account = etk_frame_new("Basic account settings");
    etk_box_append(ETK_BOX(setup_gui->vbox), setup_gui->frame_account, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);

    setup_gui->frame_account_vbox = etk_vbox_new(ETK_FALSE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account), setup_gui->frame_account_vbox);

    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("<b>Account name</b>");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->account_name_entry = etk_entry_new();
    etk_entry_text_set(ETK_ENTRY(setup_gui->account_name_entry), "Account1");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->account_name_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);

    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("<b>Full name</b>");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->full_name_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->full_name_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);

    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("<b>Email address</b>");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->email_addr_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox),setup_gui-> email_addr_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);

    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("<b>POP3 Server</b>");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->pop3_server_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->pop3_server_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);
    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("POP3 Username");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->pop3_username_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->pop3_username_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);
    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("POP3 Password");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->pop3_password_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox),setup_gui->pop3_password_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);

    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("<b>SMTP Server</b>");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->smtp_server_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->smtp_server_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);
    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("SMTP Username");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->smtp_username_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->smtp_username_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);
    setup_gui->frame_account_hbox = etk_hbox_new(ETK_TRUE, 3);
    etk_container_add(ETK_CONTAINER(setup_gui->frame_account_vbox), setup_gui->frame_account_hbox);
    setup_gui->label = etk_label_new("SMTP Password");
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->label, ETK_BOX_START, ETK_BOX_NONE, 3);
    setup_gui->smtp_password_entry = etk_entry_new();
    etk_box_append(ETK_BOX(setup_gui->frame_account_hbox), setup_gui->smtp_password_entry, ETK_BOX_START, ETK_BOX_EXPAND_FILL, 3);
    // End account setting frame

    etk_widget_show_all(setup_gui->win);

    //etk_main();
}

void _setup_quit_cb(void *data) {
    etk_main_quit();
    etk_widget_hide(ETK_WIDGET(setup_gui->win));
}

void _mbox_entry_text_changed(Etk_Widget *widget, void *data) {
    char *tmp;

    tmp = data;
    if (!str_is_alpha(tmp)) {
        etk_widget_disabled_set_all(ETK_WIDGET(setup_gui->frame_account), ETK_TRUE);
        etk_widget_disabled_set(ETK_WIDGET(setup_gui->chkbtn_skip_account_setup), ETK_TRUE);
        etk_widget_disabled_set(ETK_WIDGET(setup_gui->btn_ok), ETK_TRUE);
    } else {
        etk_widget_disabled_set_all(ETK_WIDGET(setup_gui->frame_account), ETK_FALSE);
        etk_widget_disabled_set(ETK_WIDGET(setup_gui->chkbtn_skip_account_setup), ETK_FALSE);
        etk_widget_disabled_set(ETK_WIDGET(setup_gui->btn_ok), ETK_FALSE);
    }
}

void _btn_ok_cb(Etk_Button *button, void *data) {
    Folder *folder;
    PrefsAccount *account;
    const char *tmpstr;

    Etk_Bool empty_entries= ETK_TRUE;

    if (!etk_toggle_button_active_get(ETK_TOGGLE_BUTTON(setup_gui->chkbtn_skip_account_setup))) {
        empty_entries = empty_entries && !str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->account_name_entry)));
        empty_entries = empty_entries && !str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->full_name_entry)));
        empty_entries = empty_entries && !str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->email_addr_entry)));
        empty_entries = empty_entries && !str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->pop3_server_entry)));
        empty_entries = empty_entries && !str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->smtp_server_entry)));

        if (!empty_entries) {
            msgdialog = etk_message_dialog_new(ETK_MESSAGE_DIALOG_WARNING, ETK_MESSAGE_DIALOG_OK,
                                                                            " <b>Bold</b> fields are required, and you've to use  <br>"
                                                                            " right characters.");
            etk_signal_connect("response", ETK_OBJECT(msgdialog), ETK_CALLBACK(_response_dialog_cb), NULL);
            etk_widget_show_all(msgdialog);
            return;
        }

        /* Entries are ok, now i can store it */
        account = prefs_account_new();
        /* Required fields */
        free(account->account_name);
        account->account_name = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->account_name_entry)));
        free(account->name);
        account->name = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->full_name_entry)));
        free(account->address);
        account->address = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->email_addr_entry)));
        account->protocol = A_POP3;
        free(account->recv_server);
        account->recv_server = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->pop3_server_entry)));
        free(account->smtp_server);
        account->smtp_server = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->smtp_server_entry)));
        /* Other fields */
        if (!str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->pop3_username_entry)))) {
            free(account->userid);
            account->userid = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->pop3_username_entry)));
        }
        if (!str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->pop3_password_entry)))) {
            free(account->passwd);
            account->passwd = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->pop3_password_entry)));
        }
        if (!str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->smtp_username_entry)))) {
            free(account->smtp_userid);
            account->smtp_userid = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->smtp_username_entry)));
        }
        if (!str_is_space(etk_entry_text_get(ETK_ENTRY(setup_gui->smtp_password_entry)))) {
            free(account->smtp_passwd);
            account->smtp_passwd = strdup(etk_entry_text_get(ETK_ENTRY(setup_gui->smtp_password_entry)));
        }

        /* append to internal account list */
        account_append(account);
        /* save all accounts to configuration file */
        account_write_config_all();
    }

    tmpstr = etk_entry_text_get(ETK_ENTRY(setup_gui->mbox_entry));
    etk_widget_hide(ETK_WIDGET(setup_gui->win));
    if (!tmpstr) {
        exit(1); //TODO: notify the problem
    }
    if (folder_find_from_path(tmpstr)) {
        printf("The mailbox exists.\n");
        exit(1); //TODO: notify the problem
    }
    if (!strcmp(tmpstr, "Mail")) {
        folder = folder_new(F_MH, _("Mailbox"), tmpstr);
    } else {
        folder = folder_new(F_MH, g_basename(tmpstr), tmpstr);
    }
    folder_add(folder);
    if (folder->klass->create_tree(folder) < 0) {
        printf("Creation of the mailbox failed\n");
        folder_destroy(folder);
        return;
    }

    folder->klass->scan_tree(folder);
    folder_set_missing_folders();
    folder_read_list();
    build_main_gui();
}

void _skip_account_setup_cb(Etk_Toggle_Button *button, void *data) {
    etk_widget_disabled_set_all(ETK_WIDGET(setup_gui->frame_account),
    etk_toggle_button_active_get(ETK_TOGGLE_BUTTON(button)));
}

void _response_dialog_cb(Etk_Dialog *dialog, int response_id, void *data) {
    etk_widget_hide(ETK_WIDGET(msgdialog));
}

